<?php if (!defined('BASEPATH'))exit('No direct script access allowed');

class Callback extends MY_Controller {

    function __construct() {
        parent::__construct();
    }

    //第三方qq登录回调函数
    function qq_login(){
        $this->load->library('Oauth');//qq第三方登录
        $qc = new QC();
        $access_token = $qc->qq_callback();
        $open_id = $qc->get_openid();
        $where = array();
        $where['open_id'] = $open_id;
        $users_bind = $this->users_model->get_qq_bind($where);
        $ip = $this->input->ip_address();
        if($users_bind){
            $uinfo = $this->users_model->get_user_by_uid($users_bind['user_id']);
            //登录成功，写入日志记录
            $log_data = array();
            $log_data['user_id'] = $uinfo['user_id'];
            $log_data['events'] = 'login';
            $log_data['content'] = 'qq登录成功';
            $log_data['ip'] = $ip;
            $log_data['create_time'] = time();
            $this->users_model->insert_log($log_data);
            //写入登录日志
            $login_log_data = array();
            $login_log_data['user_id'] = $uinfo['user_id'];
            $login_log_data['login_flag'] = 2;
            $login_log_data['login_ip'] = $ip;
            $login_log_data['create_time'] = time();
            $login_log_data['month'] = date("Ym");
            $login_log_data['day'] = date("Ymd");
            $this->users_model->insert_login_log($login_log_data);
            
            
            //更改用户信息
            $data = array();
            $data['login_times'] = $uinfo['login_times'] + 1;
            $data['last_ip'] = $uinfo['login_ip'];
            $data['last_time'] = $uinfo['login_time'];
            $data['login_ip'] = $ip;
            $data['login_time'] = time();
            $data['login_flag'] = 2;//登录来源(1、PC，2、微信，3、Android，4、ios)
            $this->users_model->user_update($data,$uinfo['user_id']);
            
            //跨域登录
            setcookie('tan_uuid',  encrypt_user_id($uinfo['user_id']),time()+3600*24*2, "/", ".tan.com");
            //登录
            $info = array();
            $info['user_id'] = $uinfo['user_id'];
            $info['username'] = $uinfo['username'];
            $info['mobile_flag'] = $uinfo['mobile'] ? 1 : 0;
            $info['email_flag'] = $uinfo['email'] ? 1 : 0;
            $info['last_ip'] = $data['last_ip'];
            $info['last_time'] = $data['last_time'];
            $this->session->set_userdata('userinfo', $info);
            web_redirect("account");
        }else{
            $qc1 = new QC($access_token,$open_id);
            $qc_user_info = $qc1->get_user_info();
            if($qc_user_info['ret'] == 0){
                $user_info = array(
                    'access_token' => $access_token,
                    'open_id' => $open_id,
                    'nickname' => $qc_user_info['nickname'],
                    'gender' => $qc_user_info['gender'] == "男" ? "1" : '2',
                    'province' => $qc_user_info['province'],
                    'city ' => $qc_user_info['city '],
                    'head_img_s' => $qc_user_info['figureurl_qq_1'],
                    'head_img_b' => $qc_user_info['figureurl_qq_2']
                );
                //已登录用户
                if($this->bbs_userinfo){
                    $bind_data = array();
                    $bind_data['user_id'] = $this->userinfo['user_id'];
                    $bind_data['access_token'] = $user_info['access_token'];
                    $bind_data['open_id'] = $user_info['open_id'];
                    $bind_data['nickname'] = $user_info['nickname'];
                    $bind_data['sex'] = $user_info['gender'];
                    $bind_data['province'] = $user_info['province'];
                    $bind_data['city'] = $user_info['city'];
                    $bind_data['head_img_s'] = $user_info['head_img_s'];
                    $bind_data['head_img_b'] = $user_info['head_img_b'];
                    $bind_data['create_time'] = time();
                    $this->users_model->insert_qq_bind($bind_data);
                    $info = array();
                    $info['nickname'] = $this->bbs_userinfo['nickname'] ? $this->bbs_userinfo['nickname'] : $user_info['nickname'];
                    $info['sex'] = $user_info['gender'];
                    $info['province'] = $user_info['province'];
                    $info['city'] = $user_info['province'];
                    $this->users_model->user_info_update($info,$this->userinfo['user_id']);
                    redirect('');exit;//绑定成功
                }
                
                $this->assign('type','qq');
                $this->assign('head_img', $qc_user_info['figureurl_qq_2']);
                $this->assign('nickname', $qc_user_info['nickname']);
                $this->assign('qc_info', base64_encode(json_encode($user_info)));
            }else{
                $msg = $qc_user_info['msg'];
                $url = site_url('');
                echo "<script type='text/javascript'>alert('".$msg."');window.location.href='".$url."';</script>";
            }
            $this->assign("title", "QQ账号绑定");
            $this->display('site/third.html');
        }
    }
    //第三方微博登录回调
    function weibo_login(){
        $this->load->library('Weibo');//新浪微博第三方登录
        $home = site_url("");
        session_start();
        $qc = new SaeTOAuthV2(WB_AKEY,WB_SKEY);
        if (isset($_REQUEST['code'])) {
            $keys = array();
            $keys['code'] = $_REQUEST['code'];
            $keys['redirect_uri'] = WB_CALLBACK_URL;
            try {
                    $token = $qc->getAccessToken( 'code', $keys ) ;
            } catch (OAuthException $e) {
            }
        }else{
            echo "<script type='text/javascript'>alert('访问超时，请重试！');window.location.href='".$home."';</script>";exit;
        }
        $ip = $this->input->ip_address();
        if($token){
            //判断是否已经绑定存在
            $where = array();
            $where['uid'] = $token['uid'];
            $users_bind = $this->users_model->get_weibo_bind($where);
            if($users_bind){//已绑定
                $uinfo = $this->users_model->get_user_by_uid($users_bind['user_id']);
                //登录成功，写入日志记录
                $log_data = array();
                $log_data['user_id'] = $uinfo['user_id'];
                $log_data['events'] = 'login';
                $log_data['content'] = '微博登录成功';
                $log_data['ip'] = $ip;
                $log_data['create_time'] = time();
                $this->users_model->insert_log($log_data);
                //写入登录日志
                $login_log_data = array();
                $login_log_data['user_id'] = $uinfo['user_id'];
                $login_log_data['login_flag'] = 2;
                $login_log_data['login_ip'] = $ip;
                $login_log_data['create_time'] = time();
                $login_log_data['month'] = date("Ym");
                $login_log_data['day'] = date("Ymd");
                $this->users_model->insert_login_log($login_log_data);


                //更改用户信息
                $data = array();
                $data['login_times'] = $uinfo['login_times'] + 1;
                $data['last_ip'] = $uinfo['login_ip'];
                $data['last_time'] = $uinfo['login_time'];
                $data['login_ip'] = $ip;
                $data['login_time'] = time();
                $data['login_flag'] = 2;//登录来源(1、PC，2、微信，3、Android，4、ios)
                $this->users_model->user_update($data,$uinfo['user_id']);
                //跨域登录
                setcookie('tan_uuid',  encrypt_user_id($uinfo['user_id']),time()+3600*24*2, "/", ".tan.com");
                //登录
                $info = array();
                $info['user_id'] = $uinfo['user_id'];
                $info['username'] = $uinfo['username'];
                $info['mobile_flag'] = $uinfo['mobile'] ? 1 : 0;
                $info['email_flag'] = $uinfo['email'] ? 1 : 0;
                $info['last_ip'] = $data['last_ip'];
                $info['last_time'] = $data['last_time'];
                $this->session->set_userdata('userinfo', $info);
                web_redirect("account");
            }else{
                $qc = new SaeTClientV2(WB_AKEY,WB_SKEY,$token['access_token']);
                $uid_get = $qc->get_uid();
                $uid = $uid_get['uid'];
                $qc_user_info = $qc->show_user_by_id( $uid);//根据ID获取用户等基本信息
                if(!$qc_user_info['error_code']){
                    $locat_arr = explode(" ", $qc_user_info['location']);
                    $user_info = array(
                        'access_token' => $token['access_token'],
                        'open_id' => $token['uid'],
                        'nickname' => $qc_user_info['screen_name'],
                        'gender' => $qc_user_info['gender'] == "f" ? "2" : '2',
                        'province' => $locat_arr[0],
                        'city ' => $locat_arr[1],
                        'head_img_s' => $qc_user_info['profile_image_url'],
                        'head_img_b' => $qc_user_info['avatar_hd']
                    );
                    
                    //已登录用户
                    if($this->userinfo){
                        $bind_data = array();
                        $bind_data['user_id'] = $this->userinfo['user_id'];
                        $bind_data['access_token'] = $user_info['access_token'];
                        $bind_data['uid'] = $user_info['open_id'];
                        $bind_data['nickname'] = $user_info['nickname'];
                        $bind_data['sex'] = $user_info['gender'];
                        $bind_data['province'] = $user_info['province'];
                        $bind_data['city'] = $user_info['city'];
                        $bind_data['head_img_s'] = $user_info['head_img_s'];
                        $bind_data['head_img_b'] = $user_info['head_img_b'];
                        $bind_data['create_time'] = time();
                        
                        $this->users_model->insert_weibo_bind($bind_data);
                        $info = array();
                        $info['nickname'] = $this->bbs_userinfo['nickname'] ? $this->bbs_userinfo['nickname'] : $user_info['nickname'];
                        $info['sex'] = $user_info['gender'];
                        $info['province'] = $user_info['province'];
                        $info['city'] = $user_info['province'];
                        $this->users_model->user_info_update($info,$this->userinfo['user_id']);
                        redirect('');exit;//绑定成功
                    }
                    $this->assign('type',"sina");
                    $this->assign('head_img', $qc_user_info['avatar_hd']);
                    $this->assign('nickname', $qc_user_info['screen_name']);
                    $this->assign('qc_info', base64_encode(json_encode($user_info)));
                }else{
                    $msg = $qc_user_info['error'];
                    $url = site_url('');
                    echo "<script type='text/javascript'>alert('".$msg."');window.location.href='".$url."';</script>";
                }
                $this->assign("title", "微博账号绑定");
                $this->display('site/third.html');
            }
        }
    }
    
    //微信第三方登录回调
    function weixin_login(){
        if($_REQUEST){
            $code = trim($_REQUEST['code']);
            $state = trim($_REQUEST['state']);
            if(!$code){
                echo "非法请求";die;
            }
            if($state != 1 && $state != 2){
                echo "非法参数1";die;
            }
            if($state == 2){//微信端
                header("Location:"."http://wx.tan66.com/account/callback_weixin_login.html?code=".$code);exit;
            }
            $this->load->library('Weixin');//微信第三方登录
            $wx = new Weixin();
            $token = $wx->get_access_token($code);
            $token = json_decode($token, true);
            if($token['errcode']){
                echo "授权失败，原因：".$token['errmsg'];die;
            }
            
            $ip = $this->input->ip_address();
            //判断是否已经绑定存在
            $where = array();
            $where['unionid'] = $token['unionid'];
            $users_bind = $this->users_model->get_weixin_bind($where);
            if($users_bind){
                $uinfo = $this->users_model->get_user_by_uid($users_bind['user_id']);
                //登录成功，写入日志记录
                $log_data = array();
                $log_data['user_id'] = $uinfo['user_id'];
                $log_data['events'] = 'login';
                $log_data['content'] = '微信登录成功';
                $log_data['ip'] = $ip;
                $log_data['create_time'] = time();
                $this->users_model->insert_log($log_data);
                //写入登录日志
                $login_log_data = array();
                $login_log_data['user_id'] = $uinfo['user_id'];
                $login_log_data['login_flag'] = 2;
                $login_log_data['login_ip'] = $ip;
                $login_log_data['create_time'] = time();
                $login_log_data['month'] = date("Ym");
                $login_log_data['day'] = date("Ymd");
                $this->users_model->insert_login_log($login_log_data);

                
                //更改用户信息
                $data = array();
                $data['login_times'] = $uinfo['login_times'] + 1;
                $data['last_ip'] = $uinfo['login_ip'];
                $data['last_time'] = $uinfo['login_time'];
                $data['login_ip'] = $ip;
                $data['login_time'] = time();
                $data['login_flag'] = 2;//登录来源(1、PC，2、微信，3、Android，4、ios)
                $this->users_model->user_update($data,$uinfo['user_id']);
                //跨域登录
                setcookie('tan_uuid',  encrypt_user_id($uinfo['user_id']),time()+3600*24*2, "/", ".tan.com");
                //登录
                $info = array();
                $info['user_id'] = $uinfo['user_id'];
                $info['username'] = $uinfo['username'];
                $info['mobile_flag'] = $uinfo['mobile'] ? 1 : 0;
                $info['email_flag'] = $uinfo['email'] ? 1 : 0;
                $info['last_ip'] = $data['last_ip'];
                $info['last_time'] = $data['last_time'];
                $this->session->set_userdata('userinfo', $info);
                web_redirect("account");
            }else{
                $userinfo = $wx->get_userinfo($token['access_token'], $token['openid']);
                $userinfo = json_decode($userinfo, true);
                
                if(!$userinfo['errcode']){
                    $user_info = array(
                        'unionid' => $userinfo['unionid'],
                        'open_id' => $userinfo['headimgurl'],
                        'nickname' => $userinfo['nickname'],
                        'sex' => $userinfo['sex'],
                        'province' => $userinfo["province"],
                        'city ' => $userinfo["city"],
                        'headimgurl' => $userinfo['headimgurl'],
                        'privilege' => serialize($userinfo['privilege']),
                    );
                    
                    //已登录用户
                    if($this->userinfo){
                        $bind_data = array();
                        $bind_data['user_id'] = $this->userinfo['user_id'];
                        $bind_data['unionid'] = $user_info['unionid'];
                        $bind_data['open_id'] = $user_info['open_id'];
                        $bind_data['nickname'] = $user_info['nickname'];
                        $bind_data['sex'] = $user_info['sex'];
                        $bind_data['province'] = $user_info['province'];
                        $bind_data['city'] = $user_info['city'];
                        $bind_data['headimgurl'] = $user_info['headimgurl'];
                        $bind_data['privilege'] = $user_info['privilege'];
                        $bind_data['create_time'] = time();
                        
                        $this->users_model->insert_weixin_bind($bind_data);
                        $info = array();
                        $info['nickname'] = $this->bbs_userinfo['nickname'] ? $this->bbs_userinfo['nickname'] : $user_info['nickname'];
                        $info['sex'] = $user_info['gender'];
                        $info['province'] = $user_info['province'];
                        $info['city'] = $user_info['province'];
                        $this->users_model->user_info_update($info,$this->userinfo['user_id']);
                        redirect('');exit;//绑定成功
                    }
                    $this->assign('type',"weixin");
                    $this->assign('head_img', $user_info['headimgurl']);
                    $this->assign('nickname', $user_info['nickname']);
                    $this->assign('qc_info', base64_encode(json_encode($user_info)));
                }else{
                    $msg = $userinfo['errmsg'];
                    $url = site_url('');
                    echo "<script type='text/javascript'>alert('".$msg."');window.location.href='".$url."';</script>";
                }
                $this->assign("title", "微信账号绑定");
                $this->display('site/third.html');
                
            }
        }
    }
}
